package com.itheima.fifth.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/exit")
@Api(tags = "退出登录")
public class ExitController {

    @Autowired
    private RedisTemplate redisTemplate;

   /* @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private AuthToken authToken;*/

    @GetMapping
    public void Exit(HttpServletRequest request , HttpServletResponse response) {


        Cookie[] cookies = request.getCookies();
        //System.out.println(cookies);
        //遍历cookie
        for (Cookie cookie : cookies) {
            //获取cookie的name值
            String name = cookie.getName();
            System.out.println(cookie.getValue());
            //name等于uid则从redis和cookie中删除
            if (name.equals("uid")){
                String val = cookie.getValue();
                redisTemplate.delete(val);
                cookie.setMaxAge(0);
                response.addCookie(cookie);

            }
        }
       // MultiValueMap<String, HttpCookie> cookies = request.getCookies();
//        String jtiFromCookie = getJtiFromCookie(request);
//        redisTemplate.delete(jtiFromCookie);



    }
    public String getJtiFromCookie(ServerHttpRequest request) {
        HttpCookie httpCookie = request.getCookies().getFirst("uid");
        if (httpCookie != null){
            String jti = httpCookie.getValue();
            return jti;
        }
        return null;
    }


}
